package br.com.nooclix.salon.ui.util
{
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.formatters.CurrencyFormatter;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class Utilitys
	{
		
		public function Utilitys()
		{
			
		}
		
		
		public static function getDateLabel(item:Object, column:DataGridColumn):String
		{	
			var df : DateFormatter = new DateFormatter();
			df.formatString="DD/MM/YYYY";
			
			return df.format(item[column.dataField]);
		}
		
		public static function getTimeLabel(item:Object, column:DataGridColumn):String
		{	
			var df : DateFormatter = new DateFormatter();
			df.formatString="JJ:NN";
			
			return df.format(item[column.dataField]);
		}
		
		public static function formatTime(date:Date):String
		{	
			var df : DateFormatter = new DateFormatter();
			df.formatString="JJ:NN";
			
			return df.format(date);
		}
		
		public static function formatDate(date:Date):String{
			var df : DateFormatter = new DateFormatter();
			df.formatString="DD/MM/YYYY";
			
			return df.format(date);
		}
		
		public static function formatDateByString(date:String):String{
			var df : DateFormatter = new DateFormatter();
			df.formatString="DD/MM/YYYY";
			
			return df.format(date);
		}
		
		public static function formatDateTimeByString(date:String):String{
			var df : DateFormatter = new DateFormatter();
			df.formatString="DD/MM/YYYY - JJ:NN";
			
			return df.format(date);
		}
		
		
		public static function getCurrencyLabel(item:Object, column:DataGridColumn):String
		{	
			var cf : CurrencyFormatter = new CurrencyFormatter();
			
			cf.alignSymbol = "left";
			cf.currencySymbol = "R$ ";
			cf.decimalSeparatorFrom = ".";
			cf.decimalSeparatorTo = ".";
			cf.precision = "2";
			cf.useNegativeSign = true;
			cf.useThousandsSeparator = true;
			
			return cf.format(item[column.dataField]);
			
		}
		
		public static function getPercentageLabel(item:Object, column:DataGridColumn):String
		{	
			var cf : CurrencyFormatter = new CurrencyFormatter();
			
			cf.alignSymbol = "right";
			cf.currencySymbol = " %";
			cf.decimalSeparatorFrom = ".";
			cf.decimalSeparatorTo = ".";
			cf.precision = "2";
			cf.useNegativeSign = true;
			cf.useThousandsSeparator = true;
			
			return cf.format(item[column.dataField]);
			
		}
		
		public static function formatCurrencyBR(value:Number):String{
			
			var cf : CurrencyFormatter = new CurrencyFormatter();
			cf.alignSymbol = "left";
			cf.currencySymbol = "R$ ";
			cf.decimalSeparatorFrom = ".";
			cf.decimalSeparatorTo = ".";
			cf.precision = "2";
			cf.useNegativeSign = true;
			cf.useThousandsSeparator = true;
			cf.error = "Falha ao formatar moeda";
			
			return cf.format(value); 
		}
		
		public static function formatPercentage(value:Number):String{
			
			var cf : CurrencyFormatter = new CurrencyFormatter();
			cf.alignSymbol = "right";
			cf.currencySymbol = " %";
			cf.decimalSeparatorFrom = ".";
			cf.decimalSeparatorTo = ".";
			cf.precision = "2";
			cf.useNegativeSign = true;
			cf.useThousandsSeparator = true;
			cf.error = "Falha ao formatar porcentagem";
			
			return cf.format(value); 
		}
		
		public static function toDouble(value:String):Number{
			var result:Number;
			
			var split:Array = value.split("");
			split.splice(1,1);
			
			for(var i:int=0; i < split.length; i++){
				if(split[i] == "R" || split[i] == ","){
					split.splice(i,1);
				}
			}
			
			return result = Number(split.join(""));
			
		}
		
		public static function toDoublePercentage(value:String):Number{
			var result:Number;
			
			var split:Array = value.split(" ");
			split.splice(1,1);

			return result = Number(split.join(""));
			
		}
		
		public static function unformat(value:String):String{
			
			var split:Array = value.split("");
			split.splice(1,1);
			split.splice(0,1);
			for(var i:int=0; i < split.length; i++){
				if(split[i] == "R" || split[i] == ","){
					split.splice(i,1);
				}
			}
				
			return split.join("");
		}
		
		public static function unformatPercentage(value:String):String{
			
			var split:Array = value.split(" ");
			split.splice(1,1);
			
			for(var i:int=0; i < split.length; i++){
				if(split[i] == "." || split[i] == "%" || split[i] == ","){
					split.splice(i,1);
				}
			}
			
			return split.join("");
		}

		
		public static function selectItem(searchValue:String, cb:ComboBox, property:String):Object{
			
			if(property == null){
				
				var value:String;
				var array:ArrayCollection = cb.dataProvider as ArrayCollection;
				for ( var i:int=0; i < array.length; i++){
					value = array[i]; //recupera o valor atual do item no ponteiro do data provider
					if ( value == searchValue ) { //compara o valor procurado com o valor selecionado
						cb.selectedIndex = i; //seleciona o item certo no combo box.
					}
				}
			
			}else{
				
				var value:String;
				var array:ArrayCollection = cb.dataProvider as ArrayCollection;
				
				for ( var i:int=0; i < array.length; i++){
					value = array[i][property]; //recupera o valor atual do item no ponteiro do data provider
					
					if ( value == searchValue ) { //compara o valor procurado com o valor selecionado
						cb.selectedIndex = i; //seleciona o item certo no combo box.
					}
				}
			}
			
			return cb.dataProvider;
		}
		
		
		public static function isEmpty(value:String):Boolean{
			
			if(value == null || value.length == 0 || value == " "){
				return true;
			}
			
			return false;
			
		}
		
		public static function catchErro(event:FaultEvent):void{
			WindowsMessage.error("Ops, desculpe-me pelo transtorno. Falha no servidor!\n\n" +
				"Se a falha persistir entre em contato com a equipe de suporte da Nooclix");
			
			WindowsMessage.error(event.toString());
		}
		
		
	}
}